/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.oa.service.impl;

import top.dzygod.modules.oa.domain.System;
import top.dzygod.utils.ValidationUtil;
import top.dzygod.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import top.dzygod.modules.oa.repository.SystemRepository;
import top.dzygod.modules.oa.service.SystemService;
import top.dzygod.modules.oa.service.dto.SystemDto;
import top.dzygod.modules.oa.service.dto.SystemQueryCriteria;
import top.dzygod.modules.oa.service.mapstruct.SystemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import top.dzygod.utils.PageUtil;
import top.dzygod.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzygod
* @date 2020-07-22
**/
@Service
@RequiredArgsConstructor
public class SystemServiceImpl implements SystemService {

    private final SystemRepository systemRepository;
    private final SystemMapper systemMapper;

    @Override
    public Map<String,Object> queryAll(SystemQueryCriteria criteria, Pageable pageable){
        Page<System> page = systemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(systemMapper::toDto));
    }

    @Override
    public List<SystemDto> queryAll(SystemQueryCriteria criteria){
        return systemMapper.toDto(systemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SystemDto findById(Long systemId) {
        System system = systemRepository.findById(systemId).orElseGet(System::new);
        ValidationUtil.isNull(system.getSystemId(),"System","systemId",systemId);
        return systemMapper.toDto(system);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SystemDto create(System resources) {
        return systemMapper.toDto(systemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(System resources) {
        System system = systemRepository.findById(resources.getSystemId()).orElseGet(System::new);
        ValidationUtil.isNull( system.getSystemId(),"System","id",resources.getSystemId());
        system.copy(resources);
        systemRepository.save(system);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long systemId : ids) {
            systemRepository.deleteById(systemId);
        }
    }

    @Override
    public void download(List<SystemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SystemDto system : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("系统名称", system.getName());
            map.put("逻辑删除", system.getIsDel());
            map.put("创建时间", system.getGmtCreate());
            map.put("修改时间", system.getGmtModified());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
